package com.idanchuang.component.core;

import com.idanchuang.component.core.util.InetUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.env.Environment;

/**
 * @author yjy
 * @date 2019/12/31 15:59
 **/
public class ServiceInfo {

    private static final Logger logger = LoggerFactory.getLogger(ServiceInfo.class);

    private static final String PROPERTY_APP_ID = "spring.application.name";
    private static final String PROPERTY_APP_GROUP = "app.group";
    private static final String PROPERTY_APP_VERSION = "app.version";
    private static final String PROPERTY_PORT = "server.port";
    private static final String PROPERTY_HOST = "server.address";

    private static final String DEFAULT_APP_ID = "unknown";
    private static final String DEFAULT_APP_GROUP = "default";
    private static final String DEFAULT_APP_VERSION = "-1";
    private static final String DEFAULT_PORT = "8080";
    private static final String DEFAULT_HOST = "unknown";

    /** 应用名 */
    private static String appId;
    private static String appIdLowCase;
    /** 应用ip */
    private static String host;
    /** 应用端口 */
    private static String port;
    /** 应用版本 */
    private static String version;
    /** 服务分组 */
    private static String group;

    public ServiceInfo(Environment env) {
        // appId
        ServiceInfo.appId = env.getProperty(PROPERTY_APP_ID);
        if (ServiceInfo.appId == null) {
            ServiceInfo.appId = DEFAULT_APP_ID;
            logger.info("Could not match [%appId] because [{}] config not found! use default: [{}]",
                    PROPERTY_APP_ID, DEFAULT_APP_ID);
        }
        ServiceInfo.appIdLowCase = ServiceInfo.appId.toLowerCase();
        // version
        ServiceInfo.version = env.getProperty(PROPERTY_APP_VERSION);
        if (ServiceInfo.version == null) {
            ServiceInfo.version = DEFAULT_APP_VERSION;
            logger.debug("[{}] config not set, use default: [{}]",
                    PROPERTY_APP_VERSION, DEFAULT_APP_VERSION);
        }
        // group
        ServiceInfo.group = env.getProperty(PROPERTY_APP_GROUP);
        if (ServiceInfo.group == null) {
            ServiceInfo.group = DEFAULT_APP_GROUP;
            logger.debug("[{}] config not set, use default [{}]",
                    PROPERTY_APP_GROUP, DEFAULT_APP_GROUP);
        }
        // host
        ServiceInfo.host = env.getProperty(PROPERTY_HOST);
        if (ServiceInfo.host == null) {
            ServiceInfo.host = InetUtil.getLocalIp();
            logger.debug("[{}] config not set, use localhost [{}]",
                    PROPERTY_HOST, ServiceInfo.host);
        }
        // port
        ServiceInfo.port = env.getProperty(PROPERTY_PORT);
        if (ServiceInfo.port == null) {
            ServiceInfo.port = DEFAULT_PORT;
            logger.debug("[{}] config not set, use default [{}]",
                    PROPERTY_PORT, DEFAULT_PORT);
        }
    }

    public static String address() {
        return host + ":" + port;
    }

    public static String getAppId() {
        if (appId == null) {
            return System.getenv("PAAS_APP_NAME");
        } else {
            return appId;
        }
    }

    public static String getAppIdLowCase() {
        if (appIdLowCase == null) {
            appIdLowCase = System.getenv("PAAS_APP_NAME");
            if (appIdLowCase != null) {
                appIdLowCase = appIdLowCase.toLowerCase();
            }
        }
        return appIdLowCase;
    }

    public static String getHost() {
        return host;
    }

    public static String getPort() {
        return port;
    }

    public static String getVersion() {
        return version;
    }

    public static String getGroup() {
        return group;
    }
}
